#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>

void swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}
void Adjustdown(int* b, int parent, int size)
{
	int child = parent * 2 + 1;
	while (child < size)
	{
		if (child + 1 < size && b[child + 1] > b[child])
			child++;
		if (b[parent] < b[child])
		{
			swap(&b[parent], &b[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
			return;
	}
}
void Heapsort(int* a, int size)
{
	for (int i = (size - 2) / 2; i >= 0; i--)
	{
		Adjustdown(a, i, size);
	}
	int tail = size;
	while (tail)
	{
		tail--;
		swap(&a[0], &a[tail]);
		Adjustdown(a, 0, tail);
	}
}
int main()
{
	int size = 10;
	int a[10] = { 0,1,4,7,8,5,9,6,3,2 };
	Heapsort(a, size);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
	return 0;
}